package com.flymobi.mshop.adapter;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.ProgressDialog;
import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.flymobi.mshop.R;
import com.flymobi.mshop.util.service.ClsService;
/**
 * 
 * @author 余鹏
 * 分类列表的适配器
 */
public class ClassListAdapter extends BaseExpandableListAdapter {
	private List<Map<String, Object>> groupList;
	private Map<Integer,List<Map<String, Object>>> 
	childListMap=new HashMap<Integer,List<Map<String, Object>>>();
	private List<Map<String, Object>> childList;
	private Context context;
	private TextView child;
	private TextView group;
	private View v;
	public ClassListAdapter() {
	}

	public ClassListAdapter(Context context, List<Map<String, Object>> groupList) {
		this.context = context;
		this.groupList = groupList;
	}

	public Map<String, Object> getChild(int groupPosition, int childPosition) {
		if(childListMap.get(groupPosition)!=null){
			return childListMap.get(groupPosition).get(childPosition);
		}
		return null;
	}

	public long getChildId(int groupPosition, int childPosition) {
		return childPosition;
	}

	public int getChildrenCount(int groupPosition) {
		if(childListMap.get(groupPosition)!=null){
			return childListMap.get(groupPosition).size();
		}
		return 0;
	}

	public View getChildView(int groupPosition, int childPosition,
			boolean isLastChild, View convertView, ViewGroup parent) {
		convertView = View.inflate(context, R.layout.child_cls, null);
		child = (TextView) convertView.findViewById(R.id.child);
		Map<String, Object> map = (Map<String, Object>)getChild(groupPosition,childPosition);
		child.setText(map.get("name").toString());
		return convertView;
	}

	public Map<String, Object> getGroup(int groupPosition) {
		return groupList.get(groupPosition);
	}

	public int getGroupCount() {
		return groupList.size();
	}

	public long getGroupId(int groupPosition) {
		return groupPosition;
	}

	public View getGroupView(int groupPosition, boolean isExpanded,
			View convertView, ViewGroup parent) {
		convertView = View.inflate(context, R.layout.group_cls, null);
		group = (TextView) convertView.findViewById(R.id.group);
		Map<String, Object> map = (Map<String, Object>)getGroup(groupPosition);
		group.setText(map.get("name").toString());
		return convertView;
	}

	public boolean isChildSelectable(int groupPosition, int childPosition) {
		return true;
	}

	public boolean hasStableIds() {
		return true;
	}
	
    public void onGroupCollapsed(int groupPosition) {
    }

    public void onGroupExpanded(final int groupPosition) {
    	if(getChildrenCount(groupPosition)==0){
    		mydialog = ProgressDialog.show(context, null,
    				"loading...", true);
        	mydialog.setCancelable(true);
    		new Thread() {
    			public void run() {
    				try {
    					childList = ClsService.listCls(getGroup(groupPosition).get("id").toString());
    					childListMap.put(groupPosition, childList);
    					Message msg = new Message();
    					msg.what = 0;
    					hdl.sendMessage(msg);
    				} catch (Exception e) {
    					e.printStackTrace();
    				} finally{
    					mydialog.dismiss();
    				}
    			}
    		}.start();
    	}
    }
    
    Handler hdl = new Handler() {
		public void handleMessage(android.os.Message msg) {
			if (msg.what == 0) {
				mydialog.dismiss();
				ClassListAdapter.this.notifyDataSetChanged();
				return;
			}
		};
	};
	
	private ProgressDialog mydialog;
}
